import tkinter as tk
import tkinter.ttk
import tkinter.messagebox
from CustomWidget import Virtual_Joystick

import time

from lib.log_info import LogError , LogInfo , LogWarning

s_map = [1,3,2]
ch_ratio = 660 / 82


class Virtual_Remote_Control():
    def __init__(self,out_vrc_data:dict) -> None:
        self.window = tk.Tk()
        LogInfo("虚拟遥控器窗口已创建")
        self.version = 'V1.0.0'
        LogInfo('版本已确认：' + self.version)
        self.height = 0
        self.width = 0
        self.out_vrc_data = out_vrc_data
        self.window.after(1, self.UpdateRcData)
        return
    
    def BlankFunction(self):
        '''空函数，用于占位'''
        return

    ############################################################
    #  主要功能
    #  InitApp 初始化程序
    #  RunApp 运行程序
    #  QuitApp 退出程序
    ############################################################
    def InitApp(self, width:int, height:int):
        '''初始化程序'''
        self.height = height
        self.width = width
        self.InitUI()
        LogInfo('UI已初始化')
        return
    
    def RunApp(self):
        '''运行程序'''
        self.window.mainloop()
        return

    ############################################################
    #  界面创建
    #  InitUI 初始化UI
    ############################################################
    
    def InitUI(self):
        '''初始化UI'''
        self.window.title('Virtual Remote Control  ' + self.version)
        self.window.geometry(f'{self.width}x{self.height}')
        self.window.resizable(0,0)
        
        self.ch4 = tk.Scale(self.window, from_=-660, to=660, orient=tk.VERTICAL, length=270)
        self.ch4.place(x=10, y=10)
        
        Frame_Front = tk.Frame(self.window, width=418, height=270, bd=1, relief=tk.SOLID)
        Frame_Front.place(x=80, y=10)
        # 创建虚拟拨杆
        self.s0 = tk.Scale(Frame_Front, from_=0, to=2, orient=tk.VERTICAL, length=60, showvalue=False)
        self.s0.set(1)  # Set initial value to 1
        self.s0.place(x=0, y=0)
        self.s1 = tk.Scale(Frame_Front, from_=0, to=2, orient=tk.VERTICAL, length=60, showvalue=False)
        self.s1.set(1)  # Set initial value to 1
        self.s1.place(x=390, y=0)
        # 创建虚拟摇杆
        self.v_rc_1 = Virtual_Joystick(Frame_Front, width=200, height=200, joystick_radius=10, bg="white")
        self.v_rc_1.place(x = 0, y=60)

        self.v_rc_2 = Virtual_Joystick(Frame_Front, width=200, height=200, joystick_radius=10, bg="white")
        self.v_rc_2.place(x = 210, y=60)
        return

    ############################################################
    #  实时任务
    #  UpdateRcData 更新遥控器数据
    ############################################################
    
    def UpdateRcData(self):
        '''更新遥控器数据'''
        s0 = s_map[self.s0.get()]
        s1 = s_map[self.s1.get()]
        self.out_vrc_data['s'][0] = s0
        self.out_vrc_data['s'][1] = s1
        
        ch0 =  int(self.v_rc_2.coordinate[0] * ch_ratio)
        ch1 = -int(self.v_rc_2.coordinate[1] * ch_ratio)
        ch2 =  int(self.v_rc_1.coordinate[0] * ch_ratio)
        ch3 = -int(self.v_rc_1.coordinate[1] * ch_ratio)
        ch4 = self.ch4.get()
        self.out_vrc_data['ch'][0] = ch0
        self.out_vrc_data['ch'][1] = ch1
        self.out_vrc_data['ch'][2] = ch2
        self.out_vrc_data['ch'][3] = ch3
        self.out_vrc_data['ch'][4] = ch4
        # print(self.out_vrc_data)
        self.window.after(5, self.UpdateRcData)
        return

def Task_VirtualRc(out_vrc_data:dict):
    time.sleep(1)
    v_rc = Virtual_Remote_Control(out_vrc_data)
    v_rc.InitApp(600, 400)
    v_rc.RunApp()

if __name__ == '__main__':
    out_vrc_data = {
        's':[0,0],
        'ch':[0,0,0,0,0]
    }
    Task_VirtualRc(out_vrc_data)
